import request from "@/utils/request"

export function getChannelAction () {
  return async (dispatch) => {
    const { data: { channels } } = await request.get('/channels')
    console.log('频道数据: ', channels);
    dispatch({
      type: 'channel/article',
      list: channels
    })
  }
}

/**
 * 
 * @param {*} params:{status,channel_id,begin_pubdate,end_pubdate,page,per_page}
 * @returns 
 */
export function getArticlelAction (params) {
  return async (dispatch) => {
    const { data } = await request.get('/mp/articles', {
      params
    })
    console.log('文章列表: ', data);
    dispatch({
      type: 'get/article',
      data
    })
  }
}

export function delArticleAction (id, params) {
  return async (dispatch) => {
    await request.delete(`/mp/articles/${id}`)

    dispatch(getArticlelAction(params))
  }
}

// 发布文章/存为草稿(修改复用)
/**
 * 
 * @param {*} data body数据
 * @param {*} isDraft 是否发布为草稿  true 存为草稿  false 发布文章 
 * @returns 
 */
export function publishArticleAction (data, isDraft, isEdit) {
  return async () => {
    if (!isEdit) {
      await request.post(`/mp/articles?draft=${isDraft}`, data)
    } else {
      // 修改
      await request.put(
        `/mp/articles/${data.id}?draft=${isDraft}`,
        data
      )
    }
  }
}